﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageGuest
{
    public partial class bListCategory : System.Web.UI.Page
    {
        CDanhMuc _cdm = new CDanhMuc();
        private int CurrentCategory = 0;
        private List<DanhMuc> listCategories = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCategories();
            }
        }

        private void LoadCategories()
        {
            CurrentCategory = 0;
            listCategories = _cdm.GetListCataLogIndustry();
            int end = (int)(listCategories.Count / 3) + 1;
            gvCategories.DataSource = listCategories.Take(end);
            gvCategories.DataBind();
        }

        protected void gvCategories_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (CurrentCategory >= listCategories.Count)
                return;

            GridView gvItem1 = (GridView)e.Row.FindControl("gvItem1");
            if (gvItem1 != null && CurrentCategory < listCategories.Count)
            {
                HyperLink link = (HyperLink)e.Row.FindControl("hlCategory1");
                Label lbQuantity = (Label)e.Row.FindControl("lbQuantity1");
                if (link != null && lbQuantity != null)
                {
                    if (listCategories[CurrentCategory].IDDanhMuc == 149)
                    {
                        int sl = listCategories[CurrentCategory].DanhMucMuas.Count;
                    }

                    int ID = listCategories[CurrentCategory].IDDanhMuc;
                    int quantityChildCateogry = _cdm.GetQuantityOfCategoryProduct(ID);
                    link.Text = listCategories[CurrentCategory].TenDanhMuc;
                    if (quantityChildCateogry > 0)
                    {
                        link.NavigateUrl = "~/PageGuest/bListOfCategoryLevel1.aspx?pid=" + ID;
                    }
                    else
                    {
                        link.NavigateUrl = "~/PageGuest/bListProductOfCategory.aspx?pid=" + ID;
                    }
                    lbQuantity.Text = "(" + _cdm.GetQuantityOfCatDistinctBuyOffer(ID) + ")";
                    gvItem1.DataSource = _cdm.GetChildrenOfCategory(ID, 1, 5);
                    gvItem1.DataBind();
                    if (_cdm.CheckCategoryChildrenMoreThan5(ID))
                    {
                        GridView gvItemMore = (GridView)e.Row.FindControl("gvItem1More");
                        Panel pnMoreLess = (Panel)e.Row.FindControl("pnMoreLess1");
                        if (gvItemMore != null && pnMoreLess != null)
                        {
                            pnMoreLess.Visible = true;
                            gvItemMore.DataSource = _cdm.GetChildrenOfCategory(ID, 6);
                            gvItemMore.DataBind();
                        }
                    }

                    CurrentCategory++;
                }
            }
            GridView gvItem2 = (GridView)e.Row.FindControl("gvItem2");
            if (gvItem2 != null && CurrentCategory < listCategories.Count)
            {
                HyperLink link = (HyperLink)e.Row.FindControl("hlCategory2");
                Label lbQuantity = (Label)e.Row.FindControl("lbQuantity2");
                if (link != null)
                {
                    int ID = listCategories[CurrentCategory].IDDanhMuc;
                    int quantityChildCateogry = _cdm.GetQuantityOfCategoryProduct(ID);
                    link.Text = listCategories[CurrentCategory].TenDanhMuc;
                    if (quantityChildCateogry > 0)
                    {
                        link.NavigateUrl = "~/PageGuest/bListOfCategoryLevel1.aspx?pid=" + ID;
                    }
                    else
                    {
                        link.NavigateUrl = "~/PageGuest/bListProductOfCategory.aspx?pid=" + ID;
                    }
                    lbQuantity.Text = "(" + _cdm.GetQuantityOfCatDistinctBuyOffer(ID) + ")";
                    gvItem2.DataSource = _cdm.GetChildrenOfCategory(ID, 1, 5);
                    gvItem2.DataBind();
                    if (_cdm.CheckCategoryChildrenMoreThan5(ID))
                    {
                        GridView gvItemMore = (GridView)e.Row.FindControl("gvItem2More");
                        Panel pnMoreLess = (Panel)e.Row.FindControl("pnMoreLess2");
                        if (gvItemMore != null && pnMoreLess != null)
                        {
                            pnMoreLess.Visible = true;
                            gvItemMore.DataSource = _cdm.GetChildrenOfCategory(ID, 6);
                            gvItemMore.DataBind();
                        }
                    }

                    CurrentCategory++;
                }
            }
            GridView gvItem3 = (GridView)e.Row.FindControl("gvItem3");
            if (gvItem3 != null && CurrentCategory < listCategories.Count)
            {
                HyperLink link = (HyperLink)e.Row.FindControl("hlCategory3");
                Label lbQuantity = (Label)e.Row.FindControl("lbQuantity3");
                if (link != null)
                {
                    int ID = listCategories[CurrentCategory].IDDanhMuc;
                    int quantityChildCateogry = _cdm.GetQuantityOfCategoryProduct(ID);
                    link.Text = listCategories[CurrentCategory].TenDanhMuc;
                    if (quantityChildCateogry > 0)
                    {
                        link.NavigateUrl = "~/PageGuest/bListOfCategoryLevel1.aspx?pid=" + ID;
                    }
                    else
                    {
                        link.NavigateUrl = "~/PageGuest/bListProductOfCategory.aspx?pid=" + ID;
                    }
                    lbQuantity.Text = "(" + _cdm.GetQuantityOfCatDistinctBuyOffer(ID) + ")";
                    gvItem3.DataSource = _cdm.GetChildrenOfCategory(ID, 1, 5);
                    gvItem3.DataBind();
                    if (_cdm.CheckCategoryChildrenMoreThan5(ID))
                    {
                        GridView gvItemMore = (GridView)e.Row.FindControl("gvItem3More");
                        Panel pnMoreLess = (Panel)e.Row.FindControl("pnMoreLess3");
                        if (gvItemMore != null && pnMoreLess != null)
                        {
                            pnMoreLess.Visible = true;
                            gvItemMore.DataSource = _cdm.GetChildrenOfCategory(ID, 6);
                            gvItemMore.DataBind();
                        }
                    }
                    CurrentCategory++;
                }
            }
        }
    }
}